//
//  HHChangePasswordViewController.m
//  HHBroker
//
//  Created by He Wei on 9/24/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHChangePasswordViewController.h"
#import "HttpRequestManager.h"
#import "JiaYuanShareData.h"
#import "NSString+ValidateArgument.h"

#define KAlertTagOfChangePWD 1000
@interface HHChangePasswordViewController () <UIAlertViewDelegate>
{
    BOOL _isTrueOldPwd;
    BOOL _isTrueNewPwd;

}

@property (nonatomic, weak) IBOutlet UILabel *userIDLabel;
@property (nonatomic, weak) IBOutlet UITextField *oldPasswordTextField;
@property (nonatomic, weak) IBOutlet UITextField *changePasswordTextField;
@property (nonatomic, weak) IBOutlet UIButton *returnButton;

@end

@implementation HHChangePasswordViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = NSLocalizedString(@"修改密码", nil);
    self.userIDLabel.text = [JiaYuanShareData sharedInstance].emp_rname;
}

#pragma mark - Delegate
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == KAlertTagOfChangePWD) {
        if (buttonIndex == 1) {
            __block MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hub.mode = MBProgressHUDModeIndeterminate;
            //网路请求
            //加入加密字段
            NSString *urlString = [NSString stringWithFormat:@"%@", KUrlForgetPwdGetCode];
            [HttpRequestManager getWithUrlString:urlString success:^(id response) {
                NSInteger code = [[response objectForKey:@"code"] integerValue];
                if (code == 1) {
                    //send success
                    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
                    hub.mode = MBProgressHUDModeCustomView;
                    hub.customView = imageView;
                    hub.labelText = NSLocalizedString(@"发送成功，请查看手机短信",  nil);;
                    hub.completionBlock = ^ {
                        //
                        //
                        //
                    
                    
                    };
                    [hub hide:YES afterDelay:1];
                } else {
                    hub.mode = MBProgressHUDModeText;
                    hub.labelText = [response objectForKey:@"msg"];
                    [hub hide:YES afterDelay:2];
                    
                
                }
            
            
            } failure:^(NSError *error) {
                hub.mode = MBProgressHUDModeText;
                hub.labelText = @"网路有点问题";
                [hub hide:YES afterDelay:2];
            } isLogin:YES];
        }
    }


}

#pragma mark - Event

- (IBAction)textFieldValueChanged:(UITextField *)textField
{
    if (textField == self.oldPasswordTextField) {
        _isTrueOldPwd = textField.text.length > 0 ? YES : NO;
    }
    if (textField == self.changePasswordTextField) {
        _isTrueNewPwd = textField.text.length > 0 ? YES : NO;
    }
    _returnButton.enabled = (_isTrueOldPwd && _isTrueNewPwd) ? YES : NO;
}

- (IBAction)returnButtonClicked:(UIButton *)sender
{
    [self.view endEditing:YES];
    //验证数据
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];
    NSString *oldPwd = self.oldPasswordTextField.text;
    NSString *newPwd = self.changePasswordTextField.text;
    if (![NSString validatePassword:oldPwd]) {
        alertView.title = @"愿密码必须8位以上";
        [alertView show];
        return;
    }
    if (![NSString  validatePassword:newPwd]) {
        alertView.title = @"新密码必须8位以上";
        [alertView show];
        return;
    }
    [self setNewPassword:newPwd];

}

- (IBAction)forgetPwdButtonClicked:(UIButton *)sende
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确认修改密码" message:[NSString stringWithFormat:@"我们将发送短信验证码到您的帐户绑定的手机上"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好", nil];
    alert.tag = KAlertTagOfChangePWD;
    [alert show];

}

#pragma mark - Function

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)setNewPassword:(NSString *)password
{
    NSString *oldPwd = self.oldPasswordTextField.text;
    NSString *newPwd = self.changePasswordTextField.text;
    //加入加密字段
    //
    NSString *urlString = [NSString stringWithFormat:@"%@?password=%@&new_password=%@", KUrlChangeNewPassword, oldPwd, newPwd];
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.mode = MBProgressHUDModeIndeterminate;
    
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
            hub.mode = MBProgressHUDModeCustomView;
            hub.customView = imageView;
            hub.labelText = NSLocalizedString(@"密码重置成功", nil);;
            hub.completionBlock = ^ {
                [self.navigationController popViewControllerAnimated:YES];
            
            };
            [hub hide:YES afterDelay:2];
        } else {
            NSString *msgString = [response objectForKey:@"msg"];
            hub.mode = MBProgressHUDModeText;
            hub.labelText = msgString;
            [hub hide:YES afterDelay:2];
        }
    } failure:^(NSError *error){
        hub.mode = MBProgressHUDModeText;
        hub.labelText = NSLocalizedString(@"网路好像有点问题", @"");;
        [hub hide:YES afterDelay:2];
    } isLogin:YES];
}






@end
